# 用方向键控制方块移动
# 方向键松开, 停止移动
# 碰撞后减1分, 重新碰撞再减一分, 碰到边缘减一分
# 随机多个小方块, 方块朝着玩家行走, 走直线直到走出屏幕消失
# 随机出现红色方块, 吃到红色方块会加分
# 已解决bug: 从方向A改为方向B时, 方向AB同时按下, 此时抬起A应该继续向B方向运动, 但是停止了

import random
import pygame
import sys

from pygame_test.MoveRect import MoveRect
from pygame_test.pygame_09_two_rects_02 import two_rects


def move():
    pygame.init()
    winx = 300
    winy = 300
    screen = pygame.display.set_mode((winx, winy))
    fclock = pygame.time.Clock()
    fps = 10
    speedx = 5  # 大方块运动速度x
    speedy = 5  # 大方块运动速度y
    little_speedx = 1
    little_speedy = 1
    b = 'still'  # 大方块运动状态标记
    added = False  # 是否已增加敌人模块
    f = 10  # 分数
    is_in = True  # 是否在窗口内
    pressed_direct_keys_num = 0  # 记录此时有几个方向键按下
    rect1 = MoveRect(55, 66, 20, 20, (255, 0, 255))
    little_rects = []  # 保存敌人的的rect
    red_rects = []  # 保存红色rect, 碰到后会加分
    # font = pygame.font.SysFont('SimHei', 20)
    font = pygame.font.Font(None, 20)

    while True:
        screen.fill((0, 0, 0))
        if f > 0:
            # 游戏运行时间 s
            t = int(pygame.time.get_ticks() / 1000)
            text = font.render(f'score: {f} {t}', True, (255, 255, 255))
            final_t = t
        else:
            text = font.render(f'GAMEOVER {final_t}', True, (255, 255, 255))
        screen.blit(text, (2, 2))
        if t % 5 == 0:
            # 每X秒出敌人rect
            # print(t // 30 + 1)
            if not added:
                added = True
                print('add', t)
                for i in range(t // 10 + 1):
                    # 每过10秒, 每次多增加一个敌人
                    rx = random.randint(0, winx)
                    ry = random.randint(0, winy)
                    # print (f'rx:{rx}, ry:{ry}')
                    lr = MoveRect(rx, ry, 5, 5, (255, 255, 255))
                    # 设定初始运动方向 就不再改变了
                    directx, directy = two_rects(lr.get_rect(), rect1.get_rect())
                    lr.speedx = directx * little_speedx
                    lr.speedy = directy * little_speedy
                    # print(lr.x,lr.y)
                    little_rects.append(lr)
        else:
            added = False

        # if t % 10 == 0:
        #     # 每X秒敌人加速
        #     # print(i)
        #     little_speedx += 0.1
        #     little_speedy += 0.1
        #     print(little_speedx, little_speedy)

        rect1.draw(screen)

        if len(red_rects) == 0:
            rx = random.randint(1, winx - 10)
            ry = random.randint(1, winy - 10)
            red_rects.append(MoveRect(rx, ry, 6, 6, (255, 0, 0)))
        else:
            red_rects[0].draw(screen)
            if red_rects[0].is_collide(rect1.get_rect()):
                red_rects.clear()
                f += 1

        for r in little_rects:
            # 处理敌人rect
            if not r.is_in(0, 0, winx, winy):
                # if 出界, 从列表中删除
                little_rects.remove(r)

            r.auto_move()
            r.draw(screen)
            if rect1.is_collide(r.get_rect()) and f > 0:
                if r.is_collided == False:
                    f -= 1
                    print('collide', f, r.is_collided)
                    r.is_collided = True
                    # 碰到以后小方块就消失了
                    # little_rects.remove(r)
            else:
                r.is_collided = False
        # 判断是否在屏幕内
        if not rect1.is_in(0, 0, winx, winy) and f > 0:
            if is_in:
                f -= 1
                is_in = False
                print(f)
        else:
            is_in = True
        # print(rect1.is_in(winx,winy))
        if f > 0:
            if b == 'down':
                rect1.move(0, speedy)
            elif b == 'up':
                rect1.move(0, -speedy)
            elif b == 'left':
                rect1.move(-speedx, 0)
            elif b == 'right':
                rect1.move(speedx, 0)
        else:
            print('游戏结束!')

        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_DOWN:
                    pressed_direct_keys_num += 1
                    b = 'down'
                elif e.key == pygame.K_UP:
                    pressed_direct_keys_num += 1
                    b = 'up'
                elif e.key == pygame.K_LEFT:
                    pressed_direct_keys_num += 1
                    b = 'left'
                elif e.key == pygame.K_RIGHT:
                    pressed_direct_keys_num += 1
                    b = 'right'
            elif e.type == pygame.KEYUP:
                if e.key in [pygame.K_DOWN, pygame.K_UP, pygame.K_RIGHT, pygame.K_LEFT]:
                    pressed_direct_keys_num -= 1
                    # print(f'pressed_direct_keys_num={pressed_direct_keys_num}')
                    # 只有所有方向键都抬起来的时候才停止
                    if pressed_direct_keys_num==0:
                        b = 'still'
        pygame.display.update()
        fclock.tick(fps)


if __name__ == '__main__':
    move()
